import axios from 'axios';
import { getLocalConfig } from '@/init';
import qs from 'querystring';

// 获取url参数
export const getUrlParam = (name, url) => {
  let queryUrl = url;
  if (!url) {
    queryUrl = window.location.href;
  }
  // const params = new URLSearchParams(queryUrl.substr(queryUrl.indexOf('?') + 1));
  // return params.get(name);
  const params = qs.parse(queryUrl.substr(queryUrl.indexOf('?') + 1)); // 取得所有参数
  return params[name];
};
// const host = 'http://192.168.1.75:7088/';
const host = '';

// 初始化地图查询工程态数据
export const queryGCGisDataInfo = (planId) => {
  const url = `${host}igisserver_osl/rest/gisShow2020Controller2/queryGCGisDataInfo?plan_id=${planId}`;
  // const url = `/igisserver_osl/rest/gisShow2020Controller2/queryGCGisDataInfo?plan_id=${planId}`;
  return axios.get(url);
};

// 查询信息卡
export const queryResInfo = (id, type, planId) => {
  // const url = `http://10.229.195.142:8888/oslgis2020/auditInfo/getInfoCard?res_id=${id}&model=${type}&plan_id=${planId}`;
  const url = `${host}igisserver_osl/rest/gisShow2020Controller2/getInfoCard?res_id=${id}&model=${type}&plan_id=${planId}`;
  return axios.get(url);
};

// 查询经纬度差值
export const queryLatAndLonLength = (aLat, aLon, zLat, zLon) => {
  const url = `oslgis2020/auditInfo/getLatAndLonLength?aLat=${aLat}&aLon=${aLon}&zLat=${zLat}&zLon=${zLon}`;
  return axios.get(url);
};

// 查询地图所有数据
export const queryAllGisDataInfo = (planId) => {
  const url = `${host}igisserver_osl/rest/gisShow2020Controller2/queryAllGisDataInfo?plan_id=${planId}`;
  return axios.get(url);
};

// 查询左侧树数据
export const queryFormDataInfo = (planId) => {
  // const url = `http://10.154.3.17:14000/igisserver_osl/rest/gisShow2020Controller2/queryFormDataInfo?plan_id=${planId}`;
  const url = `${host}igisserver_osl/rest/gisShow2020Controller2/queryFormDataInfoDesign?plan_id=${planId}`;
  return axios.get(url);
};

// 查询全部审核结果
export const queryResults = (planId) => {
  const url = `/oslgis2020/Examine/queryResults?plan_id=${planId}`;
  return axios.get(url);
};

export const queryPicture = async (planId, resId, useraccount) => {
  const localConfig = await getLocalConfig();
  const url = `${localConfig.URL.picture}?plan_id=${planId}&res_id=${resId}&user_account=${useraccount}`;
  console.log(url);
  return axios.get(url);
  // return requestData.data
};

// 查询光设备
export const queryGJByPlanId = (planId) => {
  const url = `/oslgis2020/GJManage/queryGJptpByplanid?plan_id=${planId}`;
  return axios.get(url);
};
// 提交审核操作和原因
export const insertResult = (params) => {
  const url = '/oslgis2020/Examine/insertResult';
  return axios.post(url, params);
};
// 提交审核操作和原因
export const allAuditResult = (params) => {
  const url = '/oslgis2020/Examine/allAuditResult';
  return axios.post(url, params);
};
// 修改审核操作和原因
export const edit = (params) => {
  const url = '/oslgis2020/Examine/updateResult';
  return axios.post(url, params);
};
function getPrefix(prefixIndex) {
  const span = '    ';
  const output = [];
  for (let i = 0; i < prefixIndex; i += 1) {
    output.push(span);
  }
  return output.join('');
}
export const str2xml = (str) => {
  let text = str;

  // 去掉多余的空格
  text = `\n${text
    .replace(
      /(<\w+)(\s.*?>)/g,
      ($0, name, props) => `${name} ${props.replace(/\s+(\w+=)/g, ' $1')}`,
    )
    .replace(/>\s*?</g, '>\n<')}`;

  // 把注释编码
  text = text
    .replace(/\n/g, '\r')
    .replace(/<!--(.+?)-->/g, ($0, TEXT) => {
      const ret = `<!--${escape(TEXT)}-->`;
      return ret;
    })
    .replace(/\r/g, '\n');

  // 调整格式
  const rgx = /\n(<(([^?]).+?)(?:\s|\s*?>|\s*?(\/)>)(?:.*?(?:(?:(\/)>)|(?:<(\/)\2>)))?)/gm;
  const nodeStack = [];
  // eslint-disable-next-line max-len
  const output = text.replace(
    rgx,
    ($0, all, name, isBegin, isCloseFull1, isCloseFull2, isFull1, isFull2) => {
      const isClosed = isCloseFull1 === '/' || isCloseFull2 === '/' || isFull1 === '/' || isFull2 === '/';
      let prefix = '';
      if (isBegin === '!') {
        prefix = getPrefix(nodeStack.length);
      } else if (isBegin !== '/') {
        prefix = getPrefix(nodeStack.length);
        if (!isClosed) {
          nodeStack.push(name);
        }
      } else {
        nodeStack.pop();
        prefix = getPrefix(nodeStack.length);
      }
      const ret = `\n${prefix}${all}`;
      return ret;
    },
  );

  // var prefixSpace = -1
  let outputText = output.substring(1);

  // 把注释还原并解码，调格式
  outputText = outputText
    .replace(/\n/g, '\r')
    .replace(/(\s*)<!--(.+?)-->/g, ($0, prefix, string1) => {
      let item = prefix;
      let Text = string1;
      if (item.charAt(0) === '\r') {
        item = item.substring(1);
      }
      Text = unescape(Text).replace(/\r/g, '\n');
      const ret = `\n${item}<!--${Text.replace(/^\s*/gm, item)}-->`;
      return ret;
    });
  // alert(outputText);

  outputText = outputText.replace(/\s+$/g, '').replace(/\r/g, '\r\n');

  return outputText;
};
export default {
  queryResInfo,
  queryResults,
  queryGJByPlanId,
  getUrlParam,
  insertResult,
  str2xml,
  getPrefix,
  edit,
};
